package cn.dyp.com.getsensordata.request;


import org.json.JSONException;
import org.json.JSONObject;

import cn.dyp.com.getsensordata.bean.SensorConfig;

/**
 * 获取所有传感器阀值的请求
 * 
 * @author asus
 *
 */
public class GetConfigRequest extends AgricultureRequest {
	// action name
	private static final String ACTION = "getConfig";

	// 存放所有传感器的阀值
	private SensorConfig sensorConfig;

	/**
	 * 获取保存数据的对象
	 * 
	 * @return SensorConfig
	 */
	public SensorConfig getSensorConfig() {
		return sensorConfig;
	}

	/**
	 * 设置保存数据的对象
	 * 
	 * @param sensorConfig
	 *            SensorConfig
	 */
	public void setSensorConfig(SensorConfig sensorConfig) {
		this.sensorConfig = sensorConfig;
	}

	/**
	 * 构造方法
	 * 
	 * @param userName
	 *            用户名
	 */
	public GetConfigRequest(String userName) {
		super(userName);
	}

	/**
	 * 获取Action的Name
	 * 
	 * @see cn.dyp.com.getsensordata.request.BaseRequest#getActionName()
	 */
	@Override
	protected String getActionName() {
		return ACTION;
	}

	/**
	 * JSON封装
	 * 
	 * @see cn.dyp.com.getsensordata.request.BaseRequest#onGetJasonBody()
	 */
	@Override
	protected String onGetJasonBody() {
		JSONObject jsonObj = new JSONObject();
		try {
			jsonObj.put("username", userName);
			return jsonObj.toString();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * JSON解析
	 * 
	 * @see cn.dyp.com.getsensordata.request.AgricultureRequest#onJasonParese(String)
	 */
	@Override
	protected void onJasonParese(String responseStr) {
		try {
			JSONObject jsonRespObj = new JSONObject(responseStr);
			if (jsonRespObj != null && sensorConfig != null) {
				if (jsonRespObj.has("result")) {
					String result = jsonRespObj.getString("result");
					// 判断请求是否成功
					if (result.equalsIgnoreCase("ok")) {
						isSuccess = true;
					} else {
						isSuccess = false;
					}
				}
				// 解析CO2浓度最大值
				if (jsonRespObj.has("maxCo2")) {
					sensorConfig.maxCo2 = jsonRespObj.getInt("maxCo2");
				}
				// 解析灯光强度最大值
				if (jsonRespObj.has("maxLight")) {
					sensorConfig.maxLight = jsonRespObj.getInt("maxLight");
				}
				// 解析CO2浓度最小值
				if (jsonRespObj.has("minCo2")) {
					sensorConfig.minCo2 = jsonRespObj.getInt("minCo2");
				}
				// 解析灯光强度最小值
				if (jsonRespObj.has("minLight")) {
					sensorConfig.minLight = jsonRespObj.getInt("minLight");
				}
				// 解析土壤湿度最大值
				if (jsonRespObj.has("maxSoilHumidity")) {
					sensorConfig.maxSoilHumidity = jsonRespObj
							.getInt("maxSoilHumidity");
				}
				// 解析土壤湿度最小值
				if (jsonRespObj.has("minSoilHumidity")) {
					sensorConfig.minSoilHumidity = jsonRespObj
							.getInt("minSoilHumidity");
				}
				// 解析空气湿度最小值
				if (jsonRespObj.has("minAirHumidity")) {
					sensorConfig.minAirHumidity = jsonRespObj
							.getInt("minAirHumidity");
				}
				// 解析空气温度最小值
				if (jsonRespObj.has("minAirTemperature")) {
					sensorConfig.minAirTemperature = jsonRespObj
							.getInt("minAirTemperature");
				}
				// 解析空气湿度最大值
				if (jsonRespObj.has("maxAirHumidity")) {
					sensorConfig.maxAirHumidity = jsonRespObj
							.getInt("maxAirHumidity");
				}
				// 解析空气温度最大值
				if (jsonRespObj.has("maxAirTemperature")) {
					sensorConfig.maxAirTemperature = jsonRespObj
							.getInt("maxAirTemperature");
				}
				// 0表示手动控制，1表示自动控制
				if (jsonRespObj.has("controlAuto")) {
					sensorConfig.controlAuto = jsonRespObj
							.getInt("controlAuto");
				}
				// 解析土壤温度最大值
				if (jsonRespObj.has("maxSoilTemperature")) {
					sensorConfig.maxSoilTemperature = jsonRespObj
							.getInt("maxSoilTemperature");
				}
				// 解析土壤温度最小值
				if (jsonRespObj.has("minSoilTemperature")) {
					sensorConfig.minSoilTemperature = jsonRespObj
							.getInt("minSoilTemperature");
				}
			} else {
				isSuccess = false;
			}
		} catch (JSONException e) {
			isSuccess = false;
			e.printStackTrace();
		}
	}

}
